import random

import pygame

from actor import WalkDir, xiao_tie
from actor.shoot_soldier import ShootSoldier
from actor.walk_soldier import WalkSoldier


class JapaneseWalkSoldier(ShootSoldier):
    def __init__(self, x, y):
        soldier_id = random.randint(1, 4)  # random是python里产生随机数的语句
        walk_image_path = "resources/images/japanese/soldier{0}.png".format(soldier_id)
        shoot_path = "./resources/images/japanese/soldier{0}-1.png".format(soldier_id)
        super(JapaneseWalkSoldier, self).__init__(walk_image_path, shoot_path, x, y, "小日本" + str(soldier_id), 100)
        self.bullets = pygame.sprite.Group()
        self.key = pygame.K_DOWN
        self.count = 5

    def run(self, down_flag, key_list, obstacle_group, bullet_group):
        if self.count == 0:
            key = random.randint(0, 3)
            self.count = 5
            if key == 0:
                self.walk_action.set_dir(WalkDir.down)
                # self.pos_y += 10
            elif key == 1:
                self.walk_action.set_dir(WalkDir.up)
                # self.pos_y -= 10
            elif key == 2:
                self.walk_action.set_dir(WalkDir.left)
                # self.pos_x -= 10
            elif key == 3:
                self.walk_action.set_dir(WalkDir.right)
                # self.pos_x += 10
        else:
            self.count -= 1

        off_x = 0
        off_y = 0
        if self.walk_action.walk_dir == WalkDir.down:
            off_y += 10
        elif self.walk_action.walk_dir == WalkDir.up:
            off_y -= 10
        elif self.walk_action.walk_dir == WalkDir.right:
            off_x += 10
        elif self.walk_action.walk_dir == WalkDir.left:
            off_x -= 10

        temp_rect = self.rect
        self.rect = self.rect.move(off_x, off_y)
        collidate_list = pygame.sprite.spritecollide(self, obstacle_group, False)
        collidate_list_bullets = pygame.sprite.spritecollide(self, bullet_group, True)
        if len(collidate_list) > 0:
            self.rect = temp_rect
        else:
            self.pos_x += off_x
            self.pos_y += off_y
        if len(collidate_list_bullets) > 0:
            # print(1)
            self.hp -= 20
        if self.hp == 0:
            self.kill()

        # super(JapaneseWalkSoldier, self).run(self.key)
